package com.gxh.controller.admin;


import com.gxh.dto.MachineDTO;
import com.gxh.dto.MachinePageQueryDTO;
import com.gxh.dto.MachineSaleDTO;
import com.gxh.result.PageResult;
import com.gxh.result.Result;
import com.gxh.service.MachineService;
import com.gxh.vo.MachineDetailAdminVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/machine")
@Api(tags = "设备管理")
@Slf4j
public class MachineController {

    @Autowired
    private MachineService machineService;

    /**
     * 设备分页查询
     * @param machinePageQueryDTO
     * @return
     */
    @GetMapping ("/page")
    @ApiOperation(value = "设备分页查询")
    public Result<PageResult> list(MachinePageQueryDTO machinePageQueryDTO){
        log.info("分页查询{}" ,machinePageQueryDTO);
        PageResult pageResult = machineService.pageQuery(machinePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增设备
     * @param machineDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增设备")
    @CacheEvict(value = "machineCache",allEntries = true)
    public Result save(@RequestBody MachineDTO machineDTO){
        log.info("新增设备{}" ,machineDTO);
        machineService.saveWithMedia(machineDTO);
        return Result.success();
    }


    @GetMapping  ("/{id}")
    @ApiOperation("根据id查询设备")
    public Result<MachineDetailAdminVO> getById(@PathVariable Long id){
        log.info("根据id查询设备{}" ,id);
        MachineDetailAdminVO machineDetailADminVO = machineService.getById(id);
        return Result.success(machineDetailADminVO);
    }

    /**
     * 修改设备
     * @param machineDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改设备")
    @CacheEvict(value = "machineCache",allEntries = true)
    public Result update(@RequestBody MachineDTO machineDTO){
        log.info("修改设备{}" ,machineDTO);
        machineService.updateWithMedia(machineDTO);
        return Result.success();
    }

    @PutMapping("/status/{status}")
    @ApiOperation("修改设备状态是否推荐")
    @CacheEvict(value = "machineCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status,@RequestParam Long id){
        log.info("修改设备状态是否推荐{}",id ,status);
        machineService.updateStatus(status,id);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("批量删除设备")
    @CacheEvict(value = "machineCache",allEntries = true)
    public Result delete(@RequestBody List<Long> ids){
        log.info("批量删除设备{}" ,ids);
        machineService.deleteBatch(ids);
        return Result.success();
    }

    @PostMapping("/sold")
    @ApiOperation("设备售出")
    @CacheEvict(value = "machineCache",allEntries = true)
    public Result sold(@RequestBody MachineSaleDTO machineSaleDTO){
        log.info("设备售出{}" ,machineSaleDTO);
        machineService.sold(machineSaleDTO);
        return Result.success();
    }

}
